In [1]:
import graphlab


A newer version of GraphLab Create (v1.7.1) is available! Your current version is v1.6.1.

You can use pip to upgrade the graphlab-create package. For more information see https://dato.com/products/create/upgrade.

Load CIFAR-10 data set


In [2]:
image_train = graphlab.SFrame('image_train_data/')


[INFO] This non-commercial license of GraphLab Create is assigned to sandipto.neogi@gmail.com and will expire on October 11, 2016. For commercial licensing options, visit https://dato.com/buy/.

[INFO] Start server at: ipc:///tmp/graphlab_server-10710 - Server binary: /Users/sandiptoneogi/anaconda/envs/dato/lib/python2.7/site-packages/graphlab/unity_server - Server log: /tmp/graphlab_server_1448084408.log
[INFO] GraphLab Server Version: 1.6.1

In [3]:
image_train.head(1)


Out[3]:
id image label deep_features image_array
24 Height: 32 Width: 32 bird [0.242871761322,
1.09545373917, 0.0, ...
[73.0, 77.0, 58.0, 71.0,
68.0, 50.0, 77.0, 69.0, ...
[1 rows x 5 columns]

Train a nearest neighbour model using deep features


In [4]:
knn_model = graphlab.nearest_neighbors.create(image_train, features=['deep_features'], label='id')


PROGRESS: Starting brute force nearest neighbors model training.

Image retrieval using deep features


In [10]:
cat = image_train[18:19]

In [11]:
graphlab.canvas.set_target('ipynb')

In [12]:
cat['image'].show()



In [13]:
knn_model.query(cat)


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 30.657ms     |
PROGRESS: | Done         |         | 100         | 259.532ms    |
PROGRESS: +--------------+---------+-------------+--------------+
Out[13]:
query_label reference_label distance rank
0 384 0.0 1
0 6910 36.9403137951 2
0 39777 38.4634888975 3
0 36870 39.7559623119 4
0 41734 39.7866014148 5
[5 rows x 4 columns]


In [17]:
def get_images_from_ids(query_result):
    return image_train.filter_by(query_result['reference_label'], 'id')

In [19]:
cat_neighbours = get_images_ids(knn_model.query(cat))


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 20.096ms     |
PROGRESS: | Done         |         | 100         | 247.07ms     |
PROGRESS: +--------------+---------+-------------+--------------+

In [20]:
cat_neighbours['image'].show()



In [21]:
car = image_train[8:9]

In [22]:
car['image'].show()



In [23]:
get_images_from_ids(knn_model.query(car))['image'].show()


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 30.164ms     |
PROGRESS: | Done         |         | 100         | 247.301ms    |
PROGRESS: +--------------+---------+-------------+--------------+

Create a lambda function


In [24]:
show_neighbours = lambda i: get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()

In [25]:
show_neighbours(8)


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 26.734ms     |
PROGRESS: | Done         |         | 100         | 250.298ms    |
PROGRESS: +--------------+---------+-------------+--------------+

In [29]:
show_neighbours(2000)


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 24.613ms     |
PROGRESS: | Done         |         | 100         | 250.336ms    |
PROGRESS: +--------------+---------+-------------+--------------+

Assignment


In [39]:
dog_sframe = image_train[image_train['label'] == 'dog']

In [40]:
len(dog_sframe)


Out[40]:
509

In [37]:
cat_sframe = image_train[image_train['label'] == 'cat']

In [38]:
len(cat_sframe)


Out[38]:
509

In [41]:
bird_sframe = image_train[image_train['label'] == 'bird']

In [42]:
len(bird_sframe)


Out[42]:
478

In [43]:
automobile_sframe = image_train[image_train['label'] == 'automobile']

In [45]:
len(automobile_sframe)


Out[45]:
509

In [53]:
print len(image_train)
print 509 * 3 + 478


2005
2005

In [54]:
dog_only_knn_model = graphlab.nearest_neighbors.create(dog_sframe, features=['deep_features'], label='id')


PROGRESS: Starting brute force nearest neighbors model training.

In [55]:
cat_only_knn_model = graphlab.nearest_neighbors.create(cat_sframe, features=['deep_features'], label='id')


PROGRESS: Starting brute force nearest neighbors model training.

In [56]:
bird_only_knn_model = graphlab.nearest_neighbors.create(bird_sframe, features=['deep_features'], label='id')


PROGRESS: Starting brute force nearest neighbors model training.

In [57]:
automobile_only_knn_model = graphlab.nearest_neighbors.create(automobile_sframe, features=['deep_features'], label='id')


PROGRESS: Starting brute force nearest neighbors model training.

In [58]:
image_test = graphlab.SFrame('image_test_data/')

In [59]:
image_test[0:1]['image'].show()



In [73]:
q2 = cat_only_knn_model.query(image_test[0:1])


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.196464    | 28.432ms     |
PROGRESS: | Done         |         | 100         | 105.853ms    |
PROGRESS: +--------------+---------+-------------+--------------+

In [74]:
q2 = q2['rank' == 1]
# Filter
cat_neighbors = image_train.filter_by(q2['reference_label'],'id')
# Get the image
cat_neighbors['image'].show()



In [79]:
q3 = dog_only_knn_model.query(image_test[0:1])


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.196464    | 20.881ms     |
PROGRESS: | Done         |         | 100         | 95.941ms     |
PROGRESS: +--------------+---------+-------------+--------------+

In [80]:
q3 = q3['rank' == 1]
# Filter
dog_neighbors = image_train.filter_by(q3['reference_label'], 'id')
# Get the image
dog_neighbors['image'].show()



In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: